package ywh.ui
{
	import com.yahoo.astra.fl.controls.Tree;
	import com.yahoo.astra.fl.controls.treeClasses.BranchNode;
	import com.yahoo.astra.fl.controls.treeClasses.LeafNode;
	import com.yahoo.astra.fl.controls.treeClasses.TNode;
	import fl.controls.listClasses.ICellRenderer;
	import fl.core.InvalidationType;
	import fl.events.InteractionInputType;
	import fl.events.ListEvent;
	import fl.events.SliderEvent;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author 杨伟华
	 */
	public class TreeMenu extends Tree
	{
		protected var _vslider:Sprite;
		private var _mouseDownY:Number;
		private var _mouseDownContentY:Number;
		private var downTime:Number;
		private var _clickTime:Number = 500;
		
		private var import_TMDP:TreeMenuDataProvider;
		private var import_Nosb:NoScrollBar;
		private var import_tnode:TNode;
		private var import_ListEvent:ListEvent;
		private var import_bnode:BranchNode;
		private var import_leafNode:LeafNode;
		
		//private var import_renderer:MyTreeCellRenderer;
		
		public function TreeMenu()
		{
			super();
			this._horizontalScrollPolicy = "off";
			this.verticalScrollPolicy = "on";
			setStyle('cellRenderer', MyTreeCellRenderer);
			addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
		}
		
		override protected function configUI():void
		{
			super.configUI();
			removeChild(_horizontalScrollBar);
			removeChild(_verticalScrollBar);
			_horizontalScrollBar = new NoScrollBar();
			_verticalScrollBar = new NoScrollBar();
			var slider:SliderForContent = new SliderForContent();
			slider.maximum = 1000;
			slider.minimum = 0;
			slider.snapInterval = 0.1;
			slider.addEventListener(MouseEvent.MOUSE_DOWN, handleSliderMouseDown);
			slider.addEventListener(SliderEvent.CHANGE, vslider_scroll);
			_vslider = slider
			_vslider.rotation = 90;
			addChild(_vslider);
		}
		
		private function vslider_scroll(e:Event):void
		{
			if ((e as SliderEvent).triggerEvent == InteractionInputType.MOUSE)
			{
				var slider:SliderForContent = _vslider as SliderForContent;
				verticalScrollPosition = slider.value / slider.maximum * maxVerticalScrollPosition;
			}
		}
		
		private function handleSliderMouseDown(e:MouseEvent):void
		{
			e.stopPropagation();
		}
		
		private function handleMouseDown(e:MouseEvent):void
		{
			addEventListener(MouseEvent.MOUSE_MOVE, handleStageMouseMove);
			addEventListener(MouseEvent.MOUSE_UP, handleStageMouseUp);
			_mouseDownY = e.stageY;
			_mouseDownContentY = this.verticalScrollPosition;
		}
		
		private function handleStageMouseMove(e:MouseEvent):void
		{
			var targetY:Number = _mouseDownY - e.stageY + _mouseDownContentY;
			
			if (targetY < 0)
			{
				verticalScrollPosition = 0;
			}
			else if (targetY > maxVerticalScrollPosition)
			{
				verticalScrollPosition = maxVerticalScrollPosition;
			}
			else
			{
				verticalScrollPosition = targetY;
			}
		}
		
		private function handleStageMouseUp(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_MOVE, handleStageMouseMove);
			removeEventListener(MouseEvent.MOUSE_UP, handleStageMouseUp);
		}
		
		override protected function setVerticalScrollPosition(scroll:Number, fireEvent:Boolean = false):void
		{
			super.setVerticalScrollPosition(scroll, fireEvent);
			var slider:SliderForContent = _vslider as SliderForContent;
			slider.value = slider.maximum * verticalScrollBar.scrollPosition / verticalScrollBar.maxScrollPosition;
		}
		
		override protected function draw():void
		{
			if (isInvalid(InvalidationType.SIZE))
			{
				_vslider.y = 50;
				_vslider.x = width;
				_vslider.width = height - 100;
			}
			super.draw();
		}
		
		override protected function drawList():void
		{
			super.drawList();
			for each (var renderer:Object in activeCellRenderers)
			{
				var rendererSprite:Sprite = renderer as Sprite;
				if (rendererSprite != null)
				{
					rendererSprite.removeEventListener(MouseEvent.CLICK, handleCellRendererClick);
					rendererSprite.removeEventListener(MouseEvent.ROLL_OVER, handleCellRendererMouseEvent);
					rendererSprite.removeEventListener(MouseEvent.ROLL_OUT, handleCellRendererMouseEvent);
					rendererSprite.removeEventListener(MouseEvent.DOUBLE_CLICK, handleCellRendererDoubleClick);
					rendererSprite.addEventListener(MouseEvent.MOUSE_DOWN, handleRendererSpriteMouseDown);
				}
			}
		}
		
		public function set clickTime(value:Number):void
		{
			_clickTime = value;
		}
		
		private function handleRendererSpriteMouseDown(event:MouseEvent):void
		{
			downTime = getTimer();
			event.target.addEventListener(MouseEvent.MOUSE_UP, handleRendererSpriteMouseUp);
		}
		
		private function handleRendererSpriteMouseUp(event:MouseEvent):void
		{
			event.target.removeEventListener(MouseEvent.MOUSE_UP, handleRendererSpriteMouseUp);
			handleStageMouseUp(null);
			var upTime:Number = getTimer();
			if ((upTime - downTime) < _clickTime)
			{
				var renderer:ICellRenderer = event.currentTarget as ICellRenderer;
				var itemIndex:uint = renderer.listData.index;
				dispatchEvent(new ListEvent(ListEvent.ITEM_CLICK, false, true, renderer.listData.column, renderer.listData.row, itemIndex, renderer.data));
			}
		}
	}

}